/*
Copyright  2018 Blockchain Technology and Application Joint Lab, Fintech Research Center of ISCAS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
import faker from 'faker/locale/zh_CN';
import db from './db'
import settings   from '../../setting'
import {CopyrightCertificates} from '../api/copyright_certificates/copyright_certificates'
import {RegisterUser} from '../api/register_user/register_user'

settings = new settings();
//fake copyright
function fakeCopyRightItem(index) {
    let c_number = new Array("655071c0-36ed-11e8-a121-bf7545026f0f", "42259b30-36ed-11e8-a121-bf7545026f0f", "11c321b0-36ed-11e8-a121-bf7545026f0f", "695f13b0-3612-11e8-a2ab-15b508085030", "d541e6d0-3611-11e8-a2ab-15b508085030",
        "fdb0b610-33f9-11e8-817a-d92b5397d1f1", "f6f1e720-33f6-11e8-817a-d92b5397d1f1", "cd183fd0-33f6-11e8-817a-d92b5397d1f1", "e332c700-33f5-11e8-817a-d92b5397d1f1", "078825c0-33f4-11e8-8fcd-2f872739aef1",
        "d5139520-33f3-11e8-8fcd-2f872739aef1", "25324820-33f0-11e8-96d5-53bceca6fc3b", "efadd4e0-33ee-11e8-96d5-53bceca6fc3b", "6486a9a0-33ee-11e8-befb-751d04961372", "deae1850-33ec-11e8-82ca-432b582d60b4",
        "7a80ea90-331c-11e8-a619-c308bfd4234f", "fc455a90-331a-11e8-ad7f-8339ba65e0a1", "b4368bc0-331a-11e8-ad7f-8339ba65e0a1", "888f7770-331a-11e8-ad7f-8339ba65e0a1", "53ff80e0-331a-11e8-ad7f-8339ba65e0a1");
    let owner_account = new Array('1MsyLUW9X4rE9cqHhQ1ro6yASCHLvaUA38', "1MH9xedPTkWThJUgT8ZYehiGCM7bEZTVGN");
    let pics_url= new Array(
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/0c653400-42ab-11e8-9d10-4780a8415e0d.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/692e1d60-42af-11e8-9a1d-75834e9350c7.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/6c487990-42ab-11e8-9d10-4780a8415e0d.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/82fb2ca0-42ab-11e8-9d10-4780a8415e0d.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/966607b0-42ab-11e8-9d10-4780a8415e0d.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/HNCK5032.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/a81772f0-42ab-11e8-9d10-4780a8415e0d.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/adrspach.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/b60a76a0-42ab-11e8-9d10-4780a8415e0d.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/boat.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/famous.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/florence.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/hairpin.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/little-girl.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/old-wooden.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/pienza-jpg.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/pienza.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/prague.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/san.jpg',
        'https://raw.githubusercontent.com/tolerjade/crbb_pics/master/vyhlidka.jpg',
        );
    let pic_des = new Array(
        '在黄色郁金香的映衬下，天空显得越发漂亮！',
        '年轻的女孩正在湖边欣赏漂亮的风景!',
        '布拉格街头的电车!',
        '葡萄酒庄园用来酿酒的葡萄已经熟了!',
        '漂亮女孩爱吃冰淇淋!',
        '貌似饥饿的小鹿正在吃着游客手中的饲料!',
        '平静的湖面有若干小船漂浮在湖边!',
        'Teplice Adrspach国家岩石公园的湖泊和岩石!',
        '夜幕映衬下的查尔斯塔!',
        '周末放松下自己，划划船、看看风景!',
        '加利福尼亚旧金山夜晚的街道!',
        '弗罗伦萨的夜晚的景色!',
        '意大利的盘山公路像发簪一样!',
        '阳光下在向日葵林中一个小女孩藏在向日葵林后面!',
        '意大利布雷斯湖边的一座老木屋!',
        '坐落于托斯卡纳的皮恩扎镇!',
        '坐落于托斯卡纳的皮恩扎镇!',
        '布拉格老城广场的天文钟!',
        '雾气笼罩下的旧金山湾!',
        '弯弯曲曲，绵延向远处的河道!'
    )
    let pic_name = new Array(
        '黄色的郁金香',
        '年轻的女孩',
        '街头电车',
        '成熟的葡萄',
        '冰淇淋女孩',
        '动物园中的小鹿',
        '湖边小船',
        '岩石湖泊',
        '查尔斯塔',
        '划船',
        '旧金山的夜晚',
        '弗罗伦萨的夜景',
        '发簪公路',
        '向日葵女孩',
        '湖边木屋',
        '欧式小镇一',
        '欧式小镇二',
        '天文钟',
        '旧金山湾',
        '弯曲的河道',
    )
    let pics_show = new Array(
        settings.pic_show_pre_url + '/seadragon/detail.html?pic=yellow&fmt=jpg&width=5000&height=3333&size=256',
        settings.pic_show_pre_url + '/seadragon/detail.html?pic=young&fmt=jpg&width=5000&height=3333&size=256',
        settings.pic_show_pre_url + '/seadragon/detail.html?pic=tram&fmt=jpg&width=5000&height=3333&size=256',
        '',
        settings.pic_show_pre_url + '/seadragon/detail.html?pic=girl&fmt=jpg&width=5000&height=3333&size=256',
        '',
        settings.pic_show_pre_url + '/seadragon/detail.html?pic=famous&fmt=jpg&width=5000&height=3333&size=256',
        '','','','','','','','','','','','',
    );



    let status = true;
    let timestamp = parseInt(Date.now() / 1000);
    let hash_algorithm = {name: 'sha256', parameter: ''}

    let pic_identity = new Array("9f5233891a9708cc983dccc238d6f26e3a42f07b798213371f0db64bfe32828a", "89baf0d3013a912640ba21c6838f2442704d1e6981e95befe6ef86d099a80879",
        "8f34349681247c727fda09fbcdf125563af235d1cb1adba175fd950c4e922210", "28f8ee1bb6d15914dc9c9a695e7285295af02382f704e925b1b50b15070ec559", "cf5ee41e95738d7c3c245afb63e6faa1fb516cff75fd26ee75b01cdd6f117bf3");

    let pic_desc = {
        author: faker.name.findName(), name: pic_name[index],
        file_size: faker.random.number(), desc: pic_des[index],
        picture_url: pics_url[index],pic_show:pics_show[index]
    };

    let createdAt = new Date();
    let fcc = {
        certificate_number: c_number[index],
        owner_account_address: owner_account[index%2],
        status: status,
        block_timestamp: timestamp,
        hash_algorithm: hash_algorithm,
        picture_digital_identity: pic_identity[index%5],
        picture_desc: pic_desc,
        createdAt: createdAt,
    };
    return fcc;
}
//add copy right
exports.addcopyrights = function() {
    for(let i=0; i<20; i++){
        let item = fakeCopyRightItem(i);
        db.copyright_certificates.where({certificate_number: item.certificate_number}).first().then((cert) => {
           if(cert){
               console.log("The item " + item.certificate_number+ " has existed!")
           } else {
               db.table('copyright_certificates').add(item).then((id)=>{
                   console.log(console.log("The item " + item.certificate_number+ " add success"))
               })
           }
        })
    }
}
//add copy right to mongo
exports.addcopyrightsToMongo = function(){
    if(CopyrightCertificates.find({}).count()  <=0 ){
        for(let i=0; i<20; i++){
            let item = fakeCopyRightItem(i);

            Meteor.call('certificate.insert', item)
        }
    }
}

//add user
exports.addUserItem = function () {
     let user = {
        phone: "18712341234",
        user_certificate_pem:"-----BEGIN CERTIFICATE-----\n" +
         "MIIDhjCCAyugAwIBAgIJAXifp6dIeqzFMAoGCCqGSM49BAMCMIIBRDEgMB4GA1UE"+
         "AwwXUGljQ29weVJpZ2h0UmVnaXN0ZXJBUFAxEjAQBgNVBAYTCXVuZGVmaW5lZDES"+
         "MBAGA1UEBwwJdW5kZWZpbmVkMRowGAYJKoZIhvcNAQkBDAsxMjNAMTIzLmNvbTGB"+
         "2zCB2AYDVQQxDIHQN2IyMjZlNjE2ZDY1MjIzYTIyNzQ2NTczNzQzMjIyMmMyMjcw"+
         "Njg2ZjZlNjUyMjNhMjIzMTM4MzczMTMyMzMzNDMxMzIzMzM0MjIyYzIyNjE2NDY0"+
         "NzI2NTczNzMyMjNhMjJlNThjOTdlNGJhYWNlNGI4YWRlNTg1YjNlNjlkOTEyMjJj"+
         "MjI3NTZlNjk3NDIyM2EyMmU4OGFiMWU2Yjk5NjIyMmMyMjY1NmQ2MTY5NmMyMjNh"+
         "MjIzMTMyMzM0MDMxMzIzMzJlNjM2ZjZkMjI3ZDAiGA8yMDE4MDEyMzA5NTkxOVoY"+
         "DzIwMTkwMTIzMDk1OTE5WjCCAUQxIDAeBgNVBAMMF1BpY0NvcHlSaWdodFJlZ2lz"+
         "dGVyQVBQMRIwEAYDVQQGEwl1bmRlZmluZWQxEjAQBgNVBAcMCXVuZGVmaW5lZDEa"+
         "MBgGCSqGSIb3DQEJAQwLMTIzQDEyMy5jb20xgdswgdgGA1UEMQyB0DdiMjI2ZTYx"+
         "NmQ2NTIyM2EyMjc0NjU3Mzc0MzIyMjJjMjI3MDY4NmY2ZTY1MjIzYTIyMzEzODM3"+
         "MzEzMjMzMzQzMTMyMzMzNDIyMmMyMjYxNjQ2NDcyNjU3MzczMjIzYTIyZTU4Yzk3"+
         "ZTRiYWFjZTRiOGFkZTU4NWIzZTY5ZDkxMjIyYzIyNzU2ZTY5NzQyMjNhMjJlODhh"+
         "YjFlNmI5OTYyMjJjMjI2NTZkNjE2OTZjMjIzYTIyMzEzMjMzNDAzMTMyMzMyZTYz"+
         "NmY2ZDIyN2QwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQjlY/BYQHUpvnNdKPE"+
         "gayqBv3Axvqyv/Q2xKdhuHCSDTZBEsL5y2mWW8lM+mroeVJcAncmzs03GT7yv+19"+
         "m4oKMAoGCCqGSM49BAMCA0kAMEYCIQCy9PyRrSPlR7hxCvHvozvrl6PRCmXUGi29"+
         "ea0VUU37QgIhALQZ7kZAMQ7oAfED4ymWu0SyHPHk1kdzJ/BqdwO3x7aQ\n" +
         "-----END CERTIFICATE-----",
        address: "北京中关村",
        unit: "花湖",
        account_address: "1MsyLUW9X4rE9cqHhQ1ro6yASCHLvaUA38",
        credential_number: "NULL",
        name: "test2",
        email: "123@123.com",
        createdAt: new Date(),
        status: true,
        avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/2fockus/128.jpg",
        isAdmin: false
    }

    Meteor.call('registerUser.insert', user)
    user['prv_key_pem_encrypted'] = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" +
        "MIHUMD8GCSqGSIb3DQEFDTAyMBoGCSqGSIb3DQEFDDANBAg31gidV2wmcwIBZDAU" +
        "BggqhkiG9w0DBwQInZ6k6Ods21AEgZASceCGSGh5mYbM495kDF17cklQNxnaK5Wl" +
        "1vTqHFZRbT0zEfL6KLcEwSnVHN2h2cVV5TY26qcaRxKbh1kWWO2J1ku1AKGe5weG" +
        "4jOHRAxMRL/iDqI4chOOaUVEqLp1cljtQpWTH/IYlL6bb+od8sfyi4rbbTUfn8Kn" +
        "oCbXjtHoCX1KRPuatSximEgyaXbgQ1I=\n" +
        "-----END ENCRYPTED PRIVATE KEY-----",
    db.users.where({phone: user.phone}).first().then((res) => {
        if (res) {
            console.log("The account has existed!")
            return;
        }
        db.table('users').add(user).then((id) => {
            console.log('add account success!')
        })
    })

}

//add admin
exports.addAdminItem = function () {
    let user = {
        phone: "13388889999",
        user_certificate_pem: "-----BEGIN CERTIFICATE-----\n" +
        "MIIEKjCCA9GgAwIBAgIJAYA5nGzpVx3VMAoGCCqGSM49BAMCMIIBlzEgMB4GA1UE" +
        "AwwXUGljQ29weVJpZ2h0UmVnaXN0ZXJBUFAxEjAQBgNVBAYTCXVuZGVmaW5lZDES" +
        "MBAGA1UEBwwJdW5kZWZpbmVkMSIwIAYJKoZIhvcNAQkBDBMxMzM4ODg4OTk5OUAx" +
        "NjMuY29tMYIBJTCCASEGA1UEMQyCARg3YjIyNmU2MTZkNjUyMjNhMjI2MTY0NmQ2" +
        "OTZlMjIyYzIyNzA2ODZmNmU2NTIyM2EyMjMxMzMzMzM4MzgzODM4MzkzOTM5Mzky" +
        "MjJjMjI2NTZkNjE2OTZjMjIzYTIyMzEzMzMzMzgzODM4MzgzOTM5MzkzOTQwMzEz" +
        "NjMzMmU2MzZmNmQyMjJjMjI2MTY0NjQ3MjY1NzM3MzIyM2EyMmU1OGM5N2U0YmFh" +
        "Y2U1Yjg4MmU2YjViN2U2Yjc4MGU1OGNiYWU0YjhhZGU1ODViM2U2OWQ5MWU1OGQ5" +
        "N2U1OWI5YmU4YTE5NzM0ZTU4ZmI3MjIyYzIyNzU2ZTY5NzQyMjNhMjJlOGJkYWZl" +
        "NGJiYjZlNjg5ODAyMjdkMCIYDzIwMTgwMTIzMDE0NTI1WhgPMjAxOTAxMjMwMTQ1" +
        "MjVaMIIBlzEgMB4GA1UEAwwXUGljQ29weVJpZ2h0UmVnaXN0ZXJBUFAxEjAQBgNV" +
        "BAYTCXVuZGVmaW5lZDESMBAGA1UEBwwJdW5kZWZpbmVkMSIwIAYJKoZIhvcNAQkB" +
        "DBMxMzM4ODg4OTk5OUAxNjMuY29tMYIBJTCCASEGA1UEMQyCARg3YjIyNmU2MTZk" +
        "NjUyMjNhMjI2MTY0NmQ2OTZlMjIyYzIyNzA2ODZmNmU2NTIyM2EyMjMxMzMzMzM4" +
        "MzgzODM4MzkzOTM5MzkyMjJjMjI2NTZkNjE2OTZjMjIzYTIyMzEzMzMzMzgzODM4" +
        "MzgzOTM5MzkzOTQwMzEzNjMzMmU2MzZmNmQyMjJjMjI2MTY0NjQ3MjY1NzM3MzIy" +
        "M2EyMmU1OGM5N2U0YmFhY2U1Yjg4MmU2YjViN2U2Yjc4MGU1OGNiYWU0YjhhZGU1" +
        "ODViM2U2OWQ5MWU1OGQ5N2U1OWI5YmU4YTE5NzM0ZTU4ZmI3MjIyYzIyNzU2ZTY5" +
        "NzQyMjNhMjJlOGJkYWZlNGJiYjZlNjg5ODAyMjdkMFkwEwYHKoZIzj0CAQYIKoZI" +
        "zj0DAQcDQgAEUuFP+C7BZ6K9odL6ZGtXzS4GE5NYTosBADX2IcBOAF2mQQxaJUyn" +
        "pBMBQ8E1AqsRmR1CYd4wwUSd7tx0VCqK1jAKBggqhkjOPQQDAgNHADBEAiBDDOTK" +
        "/XoR63APtdD5236vIr1RfUENrNNFmL59ojyzigIgbNvq72UZAFF+5L507Y+QCyf9" +
        "ZR8h6SrR5xbOIsAYers=\n" +
        "-----END CERTIFICATE-----",
        address: "北京市海淀区中关村南四街4号",
        unit: "ISCAS",
        account_address: "1MH9xedPTkWThJUgT8ZYehiGCM7bEZTVGN",
        credential_number: "NULL",
        name: "admin",
        email: "13388889999@163.com",
        createdAt: new Date(),
        status: true,
        avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/richardgarretts/128.jpg",
        isAdmin: true
    }
    Meteor.call('registerUser.insert', user)
    user['prv_key_pem_encrypted'] = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" +
        "MIHMMD8GCSqGSIb3DQEFDTAyMBoGCSqGSIb3DQEFDDANBAgG6NqYUgcI9wIBZDAU" +
        "BggqhkiG9w0DBwQI0qx+0klLj1YEgYh3JlGOthWhzLDJEBqX35N1jUb2payH//TJ" +
        "WsUCIlZuMZW/BP4OLGe9TfhvfWjPRU1mNuwE89KRqhM3k6t+ijTOaSGTpWLXzZGX" +
        "GuVANytZcnQk/F32REbgXwgF/VOrAV9z+UOS5mgYb71dQpNbBmj6WoqDX+UbJCvZ" +
        "yqfKgtXuS8bWktyY3cGN\n" +
        "-----END ENCRYPTED PRIVATE KEY-----",
    db.users.where({phone: user.phone}).first().then((res) => {
        if (res) {
            console.log("The admin account has existed!")
            return;
        }
        db.table('users').add(user).then((id) => {
            console.log('add admin account success!')
        })
    })

}

